package com.patrick.api.starter.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @author patrick
 * @date 2020/5/24 3:21 下午
 * @Des Security认证策略，部分路径不校验、资源映射配置
 * 最簡單的事是堅持，最難的事還是堅持
 */
public class WebMvcConfiguration implements WebMvcConfigurer {

  @Configuration
  @Order(101)
  public static class ApiWebSecurityConfiguration extends WebSecurityConfigurerAdapter {
    @Override
    public void configure(WebSecurity web) throws Exception {
      //配置路径忽略,authorizeRequests()配置路径访问所对应的权限，角色，认证信息等
      web.ignoring().antMatchers(
        "/error",
        "/static/**",
        "/v2/api-docs/**",
        "/swagger-resources/**",
        "/webjars/**",
        "/favicon.ico");
    }
  }

  /**
   * 自定义资源处理器
   *
   * @param registry
   */
  @Override
  public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/static/**")
      .addResourceLocations("classpath:/static/");
    registry.addResourceHandler("swagger-ui.html", "doc.html")
      .addResourceLocations("classpath:/META-INF/resources/");
    registry.addResourceHandler("/webjars/**")
      .addResourceLocations("classpath:/META-INF/resources/webjars/");
  }
}
